const api = require('../../common/api');


const fabric_query = require('../../common/service/fabric_query');
const fabric_util = require('../../common/service/fabric_util');
const myJson = require('../../common/json');


module.exports = {

    list: async function (ctx) {

        let condition = {};
        condition.Table = "department";
        if (ctx.request.query.DepartmentName !== undefined && ctx.request.query.DepartmentName !== "") {
            condition.DepartmentName = ctx.request.query.DepartmentName;
        }
        if (ctx.request.query.CompanyKey !== undefined && ctx.request.query.CompanyKey !== "") {
            condition.CompanyKey = ctx.request.query.CompanyKey;
        }
        if (ctx.request.query.DepartmentType !== undefined && ctx.request.query.DepartmentType !== "") {
            condition.DepartmentType = ctx.request.query.DepartmentType;
        }

        let args = [JSON.stringify(condition)];
        let obj = fabric_util.getObj("department", "DepartmentSelect", args)
        let res = await fabric_query.query(obj).then((res) => {
            return res;
        });
        if (myJson.isJSON(res)) {
            ctx.body = {
                "error_code": 0,
                "data": {
                    "list": JSON.parse(res)
                }
            }
        } else {
            ctx.body = {
                "error_code": 20000,
                "error_msg": res
            }
        }

    },
    add: async function (ctx) {
        if (!api.isset(ctx.request.body, ["DepartmentName", "CompanyKey", "DepartmentType"])) {
            ctx.body = {"error_code": 20000, "error_msg": "parameters is not enough!"};
            return;
        }

        let args = [JSON.stringify({
            "Table": "department",
            "DepartmentName": ctx.request.body.DepartmentName,
            "CompanyKey": ctx.request.body.CompanyKey,
            "DepartmentType": ctx.request.body.DepartmentType,
        })];
        let obj = fabric_util.getObj("department", "DepartmentAdd", args);

        let res = await fabric_query.invoke(obj).then((res) => {
            return res;
        });

        // lionel 20180821   事件机制得对invoke之后； 返回的obj是在fabric_util.getObj()获得的。执行event之前，obj应该获得怎样的值
        // end
        ctx.body = {
            error_code: 0,
            data: res
        }
    },

    update: async function (ctx) {
        if (!api.isset(ctx.request.body, ["key"])) {
            ctx.body = {"error_code": 20000, "error_msg": "parameters is not enough!"};
            return;
        }
        let condition = {};
        if (ctx.request.body.DepartmentName != undefined && ctx.request.body.DepartmentName != "") {
            condition.DepartmentName = ctx.request.body.DepartmentName;
        }
        ;
        if (ctx.request.body.CompanyKey != undefined && ctx.request.body.CompanyKey != "") {
            condition.CompanyKey = ctx.request.body.CompanyKey;
        }
        ;
        if (ctx.request.body.DepartmentType != undefined && ctx.request.body.DepartmentType != "") {
            condition.DepartmentType = ctx.request.body.DepartmentType;
        }
        ;
        let args = [ctx.request.body.key, JSON.stringify(condition)];
        let obj = fabric_util.getObj("department", "DepartmentUpdate", args);

        let res = await fabric_query.invoke(obj).then((res) => {
            return res;
        });
        ctx.body = {
            error_code: 0,
            data: res
        }
    },

    del: async function (ctx) {
        if (!api.isset(ctx.request.body, ["key"])) {
            ctx.body = {"error_code": 20000, "error_msg": "parameters is not enough!"};
            return;
        }

        let args = [ctx.request.body.key];
        let obj = fabric_util.getObj("department", "DepartmentDel", args)

        let res = await fabric_query.invoke(obj).then((res) => {
            return res;
        });
        ctx.body = {
            error_code: 0,
            data: res
        }
    }
};